const PositionDao = require('../dao/position-dao')
const PositionService={
    add(req,res,next){
        //POST
        const{company,position,salary,address}=req.body;
        //上传图片的文件名
        let logo='';
        if(req.file){
            logo = '/images/upload/'+req.file.filename;
        }

        //保存到数据库
        PositionDao.save({company,position,salary,address,logo}).then((data)=>{
        res.json({
                res_code:1,
                res_error:'',
                res_body:{
                    status:1,
                    data:data
                }
        })
    }).catch((err)=>{
        res.json({
            res_code:1,
            res_error:'',
            res_body:{
                status:0,
                data:err
            }
        })
    })
    
    },
    //分页
    findPage(req,res,next){
        var {page=1} =req.query; 
        const result = PositionDao.findByPage(page);
        
          console.log(result);//promise pending
          result.then((result)=>{
            res.json({
                totalPage:result.totalPage,
                currentPage:result.currentPage,
                    res_code:1,
                    res_error:'',
                    res_body:{
                        status:1,
                        main:result.doc
                    }
            })
          })
               
       
    }
}

module.exports=PositionService;